//
//  GraphViewController.m
//  CollectionManagerFlow
//
//  Created by Sophia Tsay on 5/2/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "GraphViewController.h"
#import "ColorUtility.h"
#import "CustomerDetailTableViewController.h"

@interface GraphViewController ()
@property (strong, nonatomic) IBOutlet UIToolbar *toolbar;

@end

@implementation GraphViewController
@synthesize graphImageView = _graphImageView;
@synthesize notesButton = _notesButton;
@synthesize detailsButton = _detailsButton;
@synthesize buttonsView = _buttonsView;
@synthesize toolbar = _toolbar;
@synthesize splitViewBarButtonItem = _splitViewBarButtonItem;
@synthesize graphName = _graphName;
@synthesize objectType = _objectType;

- (void) setNotesButton:(UIButton *)notesButton
{
    _notesButton = notesButton;
    CAGradientLayer *gradientLayer = [ColorUtility buttonGradientLayer];
        
    gradientLayer.frame = _notesButton.layer.bounds;
    [_notesButton.layer insertSublayer:gradientLayer atIndex:0];
    _notesButton.titleLabel.textColor = [UIColor whiteColor];
}

- (void) setDetailsButton:(UIButton *)detailsButton
{
    _detailsButton = detailsButton;
    CAGradientLayer *gradientLayer = [ColorUtility buttonGradientLayer];
    
    gradientLayer.frame = _detailsButton.layer.bounds;
    [_detailsButton.layer insertSublayer:gradientLayer atIndex:0];
    _detailsButton.titleLabel.textColor = [UIColor whiteColor];
}

- (void) setButtonsView:(UIView *)buttonsView
{
    _buttonsView = buttonsView;
    
    CAGradientLayer *gradientLayer = [ColorUtility toolBarGradientLayer];
    
    gradientLayer.frame = _buttonsView.layer.bounds;
    [_buttonsView.layer insertSublayer:gradientLayer atIndex:0];
}

- (void) setToolbar:(UIToolbar *)toolbar
{
    _toolbar = toolbar;
    self.toolbar.backgroundColor = [UIColor lightGrayColor]; 
}

- (void) setGraphImageView:(UIImageView *)graphImageView
{
    if (!self.graphName) {
        graphImageView.backgroundColor = [UIColor blackColor];
    } else {
        NSString *filePath = [[NSBundle mainBundle] pathForResource:self.graphName ofType:@"jpg"];    
        UIImage *graphImage = [[UIImage alloc] initWithContentsOfFile:filePath];
        graphImageView.image = graphImage;
    }
    /*
    if ([self.graphName isEqualToString:@"sunburst_delinquency"]) {
        UIButton *addCollectorsButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        addCollectorsButton.frame = CGRectMake(500, 170, 160, 60);
        [addCollectorsButton setTitle:@"Add Collectors" forState:UIControlStateNormal];
        addCollectorsButton.backgroundColor = [UIColor blackColor];
        self.graphImageView.backgroundColor = [UIColor clearColor];
        [self.graphImageView.superview addSubview:addCollectorsButton];
        [self.graphImageView.superview bringSubviewToFront:addCollectorsButton];
    }
     */
    _graphImageView = graphImageView;
}

- (void) setSplitViewBarButtonItem:(UIBarButtonItem *)splitViewBarButtonItem
{
    if (_splitViewBarButtonItem != splitViewBarButtonItem) {
        NSMutableArray *toolBarItems = [self.toolbar.items mutableCopy];
        if (_splitViewBarButtonItem) [toolBarItems removeObject:_splitViewBarButtonItem];
        if (splitViewBarButtonItem) [toolBarItems addObject:splitViewBarButtonItem];
        self.toolbar.items = toolBarItems;
        _splitViewBarButtonItem = splitViewBarButtonItem;        
    }
    if (self.toolbar.items.count == 0 && self.toolbar.hidden == NO)
    {
    	self.toolbar.hidden = YES;
        [self.graphImageView setFrame:CGRectMake(self.graphImageView.frame.origin.x, self.graphImageView.frame.origin.y - self.toolbar.frame.size.height, self.graphImageView.frame.size.width, self.graphImageView.frame.size.height + self.toolbar.frame.size.height)];
    } else if (self.toolbar.items.count != 0 && self.toolbar.hidden == YES) {
        self.toolbar.hidden = NO;
        [self.graphImageView setFrame:CGRectMake(self.graphImageView.frame.origin.x, self.graphImageView.frame.origin.y + self.toolbar.frame.size.height, self.graphImageView.frame.size.width, self.graphImageView.frame.size.height - self.toolbar.frame.size.height)];
    }

}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
/*
- (void)loadView
{
    // If you create your views manually, you MUST override this method and use it to create your views.
    // If you use Interface Builder to create your views, then you must NOT override this method.
}
*/
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [self setToolbar:nil];
    [self setGraphImageView:nil];
    [self setNotesButton:nil];
    [self setDetailsButton:nil];
    [self setButtonsView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	return YES;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"graphToDetailSegue"])
    {
        CustomerDetailTableViewController *collectorVC = (CustomerDetailTableViewController *)segue.destinationViewController;
        collectorVC.objectType = self.objectType;
        NSLog(@"object type in graph view controller is %@", self.objectType);
    } 
}
@end
